/*
 * @Author: sjf 1758142861@qq.com
 * @Date: 2023-03-14 18:00:29
 * @LastEditors: sjf 1758142861@qq.com
 * @LastEditTime: 2023-03-16 17:41:15
 * @FilePath: /D_C_lib/lw_process/mythread.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<cstdio>
#include<string>
#include<cassert>
#include<unistd.h>
#include<pthread.h>

using namespace std;

int g_val = 0;

std::string func()
{
    return "我是一个独立的方法";
}

void *thread_routine(void* args)
{
    //func();
    const char* name = static_cast<const char*>(args);
    while(true)
    {
        // std::cout << "我是新进程,我正在运行! name:" << name << " : " << func() << " : " << "g_val: " << g_val++ << "&g_val:" << &g_val << std::endl;
        std::cout << "我是新进程,我正在运行! name:" << name << " : "
         << g_val++ << "&g_val:" << &g_val << std::endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    int n = pthread_create(&tid,nullptr,thread_routine,(void*)"thread one");
    assert(0 == n);
    (void)n;
    //主线程
    while(true)
    {
        char tidbuffer[64];
        snprintf(tidbuffer,sizeof(tidbuffer),"0x%x",tid);
        std::cout << " 我是主线程,我正在运行,我创建出来的线程tid:" << tidbuffer << " : " << g_val 
        << "&g_val:" << &g_val << std::endl;
        sleep(1);
    }
    return 0;
}